//
//  tagScrollView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class TagScrollView: UIScrollView {

    private var tapClosure: ((NSInteger) -> Void)?
    private var labelColor: UIColor?
    private var fontSize: NSInteger?
    let titleLabelSideMargin: CGFloat = 12.5
    func showData(titles: [String], labelColor: UIColor, fontSize: NSInteger, tapClosure: @escaping (NSInteger) -> Void) {
        self.tapClosure = tapClosure
        self.labelColor = labelColor
        self.fontSize = fontSize
        let titleLabel = UILabel()
        titleLabel.text = "标签:"
        titleLabel.textColor = ONColor.normalTextColor
        titleLabel.font = UIFont.systemFont(ofSize: 13)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(titleLabelSideMargin)
            make.centerY.equalTo(self.snp.centerY)
        }
        titleLabel.sizeToFit()
        
        var beforButtonWidth: CGFloat = titleLabel.width + 7 + titleLabelSideMargin
        let buttonSideMargin = 10
        for (index, item) in titles.enumerated() {
            let tagButton = BaseCircularButton(title: item, fontSize: self.fontSize!, titleSideOffset: 6.5, titleColor: self.labelColor!, borderColor: ONColor.tagLineColor)
            tagButton.frame = CGRect(origin: CGPoint(x: beforButtonWidth + CGFloat(index * buttonSideMargin), y: 0), size: tagButton.buttonSize)
            tagButton.centerY = self.height / 2
            beforButtonWidth += tagButton.width
            tagButton.tag = index
            tagButton.addTarget(self, action: #selector(tapTag(_:)), for: .touchUpInside)
            self.addSubview(tagButton)
        }
    }
    
    @objc func tapTag(_ btn: BaseCircularButton) {
        if let tapClosure = self.tapClosure {
            tapClosure(btn.tag)
        }
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
